/**
 * Created by Administrator on 2016/3/2.
 */
gpNamespace.ZjdLayer = cc.Layer.extend({
	freeBt: null,
	yindan1Avi: null,
	yindan2Avi: null,
	yellowBack: null,
	btBg: null,
	time: null,
	ctor: function () {
		this._super();

		this.init();
	},
	init: function () {
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.zjdRes.Zjd_plist);
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.sctcRes.Sctc_plist);
//		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkzjdRes.Zjd_plist);
		
		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.yindandan1AnimationRes.Yindandan1_png,
				gpNamespace.yindandan1AnimationRes.Yindandan1_plist,
				gpNamespace.yindandan1AnimationRes.Yindandan1_json);
		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.yindandan2AnimationRes.Yindandan2_png,
				gpNamespace.yindandan2AnimationRes.Yindandan2_plist,
				gpNamespace.yindandan2AnimationRes.Yindandan2_json);

		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.golddan1AnimationRes.Golddan1_png,
				gpNamespace.golddan1AnimationRes.Golddan1_plist,
				gpNamespace.golddan1AnimationRes.Golddan1_json);
		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.golddan2AnimationRes.Golddan2_png,
				gpNamespace.golddan2AnimationRes.Golddan2_plist,
				gpNamespace.golddan2AnimationRes.Golddan2_json);

		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.shilianza1AnimationRes.shilianza1_png,
				gpNamespace.shilianza1AnimationRes.shilianza1_plist,
				gpNamespace.shilianza1AnimationRes.shilianza1_json);
		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.shilianza2AnimationRes.shilianza2_png,
				gpNamespace.shilianza2AnimationRes.shilianza2_plist,
				gpNamespace.shilianza2AnimationRes.shilianza2_json);

		var size = cc.winSize;

		//背景图层
		var background = new cc.Sprite(gpNamespace.zjd.Zjd_Bg);
		background.attr({
			x: 66 + background.width / 2,
			y: 94 + background.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(background);


		/*顶部label*/
		//100话费
		var topLabel1 = new cc.Sprite(gpNamespace.zjd.Zjd_100M);
		topLabel1.attr({
			x: 117 + topLabel1.width / 2,
			y: 522 + topLabel1.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(topLabel1);
		//50000券
		var topLabel2 = new cc.Sprite(gpNamespace.zjd.Zjd_5000M);
		topLabel2.attr({
			x: 501 + topLabel2.width / 2,
			y: 522 + topLabel2.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(topLabel2);
		//50000券
		var topLabel3 = new cc.Sprite(gpNamespace.zjd.Zjd_5000M);
		topLabel3.attr({
			x: 895 + topLabel3.width / 2,
			y: 522 + topLabel3.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(topLabel3);

		/*底部label*/
		//随机道具
		var bottomLabel1 = new cc.Sprite(gpNamespace.zjd.Zjd_Random);
		bottomLabel1.attr({
			x: 93 + bottomLabel1.width / 2,
			y: 192 + bottomLabel1.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(bottomLabel1);
		//必券
		var bottomLabel2 = new cc.Sprite(gpNamespace.zjd.Zjd_Must);
		bottomLabel2.attr({
			x: 527 + bottomLabel2.width / 2,
			y: 192 + bottomLabel2.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(bottomLabel2);
		//必券
		var bottomLabel3 = new cc.Sprite(gpNamespace.zjd.Zjd_Must);
		bottomLabel3.attr({
			x: 923 + bottomLabel3.width / 2,
			y: 192 + bottomLabel3.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(bottomLabel3);


		//顶部三个按钮
		var chargeQuickly = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_QuicklyChargeBt),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_QuicklyChargeBt),
				this.gotoScCallback, this);
		chargeQuickly.setUserData(4);

		var changeGold = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_ChangeGoldBt),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_ChangeGoldBt),
				this.gotoScCallback, this);
		changeGold.setUserData(2);

		var chargeReal = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_ChargeRealBt),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_ChargeRealBt),
				this.gotoScCallback, this);
		chargeReal.setUserData(1);

		var topBtMenu = new cc.Menu(chargeQuickly, changeGold, chargeReal);

		topBtMenu.attr({
			x: size.width / 2,
			y: 576 + chargeQuickly.height / 2,
		});
		topBtMenu.alignItemsHorizontallyWithPadding(190);
		this.addChild(topBtMenu, 2);

		//金蛋
		var egg1 = new cc.MenuItemImage(gpNamespace.zjd.Zjd_Egg1, gpNamespace.zjd.Zjd_Egg1, this.egg1CallbackFunction, this);
		var egg2 = new cc.MenuItemImage(gpNamespace.zjd.Zjd_Egg2, gpNamespace.zjd.Zjd_Egg2, this.egg2CallbackFunction, this);
		var egg3 = new cc.MenuItemImage(gpNamespace.zjd.Zjd_Egg3, gpNamespace.zjd.Zjd_Egg3, this.egg3CallbackFunction, this);
		var eggsMenu = new cc.Menu(egg1, egg2, egg3);

		eggsMenu.attr({
			x: size.width / 2+10,
			y: 216 + egg2.height / 2,
		});
		eggsMenu.alignItemsHorizontallyWithPadding(190);
		this.addChild(eggsMenu, 2);

		//底部三个按钮
		//第一个免费按钮有bug，暂未解决
		var freeBt = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_Free),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_Free),
				this.freeBtCallbackFunction, this);
		freeBt.visible = false;
		this.freeBt = freeBt;
		var btBg = new cc.Sprite(gpNamespace.zjd.Zjd_Touched);
		btBg.attr({
			x:116,
			y:30,
			anchorX:0,
			anchorY:0
		});
		this.btBg = btBg;
		this.addChild(btBg,1);
//		var noEggTTF = new cc.LabelTTF("已砸",gpNamespace.DEFAULTFON,30);
//		noEggTTF.setFontFillColor(cc.color.WHITE);
//		noEggTTF.attr({
//		x: 110,
//		y: 70,
//		anchorX:0,
//		anchorY:0
//		});
//		btBg.addChild(noEggTTF);

		var times10 = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_10Times),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_10Times),
				this.times10CallbackFunction, this);

		var times1 = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.zjd.Zjd_1Times),
				new tyNamespace.maskSprite(gpNamespace.zjd.Zjd_1Times),
				this.times1CallbackFunction, this);

		var bottomMenu = new cc.Menu(freeBt, times10, times1);

		bottomMenu.attr({
			x: size.width / 2+10,
			y: 30 + freeBt.height / 2,
		});
		bottomMenu.alignItemsHorizontallyWithPadding(110);
		this.addChild(bottomMenu, 2);

		gpNamespace.TyNetManager.getInstance().setEggLayer(this);
		xinyou.netManager.canEgg();//检查能否砸蛋
	},

	freeBtCallbackFunction:function(sender){
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		tyNamespace.setMenuEnable(sender);
		var self = this;
		tyNamespace.duangDuangFirst(sender,function () {
			cc.log("点击免费砸金蛋!");
			var yindan1Avi = new ccs.Armature("yindandan_001u");
			yindan1Avi.getAnimation().playWithIndex(0);
			yindan1Avi.getAnimation().setSpeedScale(1);
			yindan1Avi.setScale(1);
			yindan1Avi.setAnchorPoint(0.5, 0.5);
			yindan1Avi.setPosition(cc.winSize.width/2+10,cc.winSize.height/2+10);
			self.yindan1Avi = yindan1Avi;
			self.addChild(yindan1Avi,3);
			yindan1Avi.getAnimation().setMovementEventCallFunc(self.freeEggCallBackRemove, self);
			self.freeBt.visible = false;
			userInfo.isKickEgg = true;
		});
	},
	freeEggCallBackRemove: function (armature, movementType, movementID) {
		if (movementType == ccs.MovementEventType.complete) {
			armature.removeFromParent(true);
		}
		var yindan2Avi = new ccs.Armature("yindandan_002u");
		yindan2Avi.getAnimation().playWithIndex(0);
		yindan2Avi.getAnimation().setSpeedScale(1);
		yindan2Avi.setScale(1);
		yindan2Avi.setAnchorPoint(0.5, 0.5);
		yindan2Avi.setPosition(cc.winSize.width/2+70,cc.winSize.height/2+60);
		this.yindan2Avi = yindan2Avi;
		this.addChild(yindan2Avi,5);
		yindan2Avi.getAnimation().setMovementEventCallFunc(null, this);

		this.scheduleOnce(function(){
			xinyou.netManager.egg();
			xinyou.netManager.getRestTime(1);//获得金蛋倒计时
		},1);
	},

	times10CallbackFunction: function(sender){
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		tyNamespace.setMenuEnable(sender);
		var self = this;
		tyNamespace.duangDuangFirst(sender,function () {
			if(userInfo.gold < 100000){
				tyNamespace.warningJumper(1,tyNamespace.WarningSectencesIndex.NoGold);
			}else{
				cc.log("点击付费砸金蛋10次!");
				var yindan1Avi = new ccs.Armature("shilianza_001u");
				yindan1Avi.getAnimation().playWithIndex(0);
				yindan1Avi.getAnimation().setSpeedScale(1);
				yindan1Avi.setScale(1);
				yindan1Avi.setAnchorPoint(1, 0.5);
				yindan1Avi.setPosition(cc.winSize.width/2+35,cc.winSize.height/2+10);
				self.yindan1Avi = yindan1Avi;
				self.addChild(yindan1Avi,3);
				yindan1Avi.getAnimation().setMovementEventCallFunc(self.times10CallbackCallBackRemove, self);
			}
		});
	},
	times10CallbackCallBackRemove:function(armature, movementType, movementID){
		if (movementType == ccs.MovementEventType.complete) {
			armature.removeFromParent(true);
		}
		var yindan2Avi = new ccs.Armature("shilianza_002u");
		yindan2Avi.getAnimation().playWithIndex(0);
		yindan2Avi.getAnimation().setSpeedScale(1);
		yindan2Avi.setScale(1);
		yindan2Avi.setAnchorPoint(1, 0.5);
		yindan2Avi.setPosition(cc.winSize.width/2+245,cc.winSize.height/2+60);
		this.yindan2Avi = yindan2Avi;
		this.addChild(yindan2Avi,3);
		yindan2Avi.getAnimation().setMovementEventCallFunc(null, this);

		this.scheduleOnce(function(){
			yindan2Avi.runAction(new cc.FadeOut(1));
		},2);
		this.scheduleOnce(function(){
			xinyou.netManager.goldEgg(10);
		},1);
	},
	times1CallbackFunction: function(sender){
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		tyNamespace.setMenuEnable(sender);
		var self = this;
		tyNamespace.duangDuangFirst(sender,function () {
			if(userInfo.gold < 10000){
				//  		this.fadeInThenOut();
				tyNamespace.warningJumper(1,tyNamespace.WarningSectencesIndex.NoGold);
			}else{
				cc.log("点击付费砸金蛋1次!");
				var yindan1Avi = new ccs.Armature("golddan_001u");
				yindan1Avi.getAnimation().playWithIndex(0);
				yindan1Avi.getAnimation().setSpeedScale(1);
				yindan1Avi.setScale(1);
				yindan1Avi.setAnchorPoint(1, 0.5);
				yindan1Avi.setPosition(cc.winSize.width/2+35,cc.winSize.height/2+10);
				self.yindan1Avi = yindan1Avi;
				self.addChild(yindan1Avi,3);
				yindan1Avi.getAnimation().setMovementEventCallFunc(self.times1CallbackCallBackRemove, self);
			}
		});
	},

	times1CallbackCallBackRemove: function (armature, movementType, movementID) {
		if (movementType == ccs.MovementEventType.complete) {
			armature.removeFromParent(true);
		}
		var yindan2Avi = new ccs.Armature("golddan_002u");
		yindan2Avi.getAnimation().playWithIndex(0);
		yindan2Avi.getAnimation().setSpeedScale(1);
		yindan2Avi.setScale(1);
		yindan2Avi.setAnchorPoint(1, 0.5);
		yindan2Avi.setPosition(cc.winSize.width/2+245,cc.winSize.height/2+60);
		this.yindan2Avi = yindan2Avi;
		this.addChild(yindan2Avi,5);
		yindan2Avi.getAnimation().setMovementEventCallFunc(null, this);

		xinyou.netManager.goldEgg(1);
	},

	showYelllowBg:function(){
		var yellowBack = new cc.Sprite(gpNamespace.zjd.Zjd_YellowCricle_Bg);
		yellowBack.attr({
			x: 640,
			y: 360
		});
		this.yellowBack = yellowBack;
		this.addChild(this.yellowBack,4);
		this.yellowBack.runAction(cc.repeatForever(cc.rotateBy(4, 360)));

		this.scheduleOnce(function(){
			this.yellowBack && this.yellowBack.runAction(new cc.FadeOut(1));
			this.yindan2Avi && this.yindan2Avi.runAction(new cc.FadeOut(1));
		},2);
	},

	gotoScCallback:function(sender){
		xinyou.netManager.inPhysicalExchange();//获取商城实物奖品的信息
		cc.log("狗to商城");
		var index = sender.getUserData();
		cc.log("index:"+index);
		tyNamespace.setMenuEnable(sender);
		tyNamespace.duangDuangFirst(sender,function () {
			var runningScene = cc.director.getRunningScene();
			var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
			var modelLayer = new tyNamespace.ModelLayer(70);
			modelLayer.setTouchArea(cc.size(1210, 570), cc.p(35, 35));
			modelLayerCancelable.addChild(modelLayer);
			runningScene.addChild(modelLayerCancelable, 10);
			var scLayer = new gpNamespace.ScLayer(index);
			scLayer.attr({
				//左上角坐标
				x: 0,
				y: cc.winSize.height
			});
			modelLayer.addChild(scLayer);
			tyNamespace.AnimationManager.nodeJumpShow(scLayer, -cc.winSize.height);
		});
	},

	//控制第一个免费是否可砸
	setCanEgg:function(){
		this.freeBt.visible = true;
	},

	//设置免费砸金蛋倒计时
	setEggTime: function (time) {
		this.time = time;

		if (this.time != 0) {
			var self = this;
			this.timeCallbackFunction(this);
			this.btBg.schedule(function(){
				self.timeCallbackFunction(self);
			}, 1);
		}
		else {
			this.freeItem.setVisible(true);
			this.noFree.setVisible(false);
		}
//		this.getParent().getParent().setCloseable(true);//设置本页面可关闭
	},

	timeCallbackFunction: function (self) {
		this.time--;

		var formateTime = tyNamespace.FormatSeconds(this.time);
//		cc.log("time:" + formateTime);
		var timeTTF = new cc.LabelTTF(formateTime, gpNamespace.DEFAULTFONT, 35);
		timeTTF.attr({
			x: 140,
			y: 85,
			anchorX: 0.5,
			anchorY: 0.5
		});
		this.timeTTF = timeTTF;
//		this.timeTTF.runAction(cc.sequence(cc.fadeOut(1), cc.fadeIn(1)));
		this.btBg.removeAllChildren(true)
		this.btBg.addChild(timeTTF);

		if (this.time == 0) {
			this.setCanEgg();
			this.unschedule(this.btBg);
		}
	},

	showRewards:function(rewardtype,num,y){
		this.showYelllowBg();
		var heigth = cc.winSize.height/2;
		if(y){
			heigth = y;
		}
//		cc.log("奖励表现");
		var rewardsPicpath = null;
		switch (rewardtype) {
		case 1:
//			cc.log("获得金币："+egg.reward[i][1]);
			rewardsPicpath = gpNamespace.sctc.Sctc_Gold;
			break;
		case 2:
//			cc.log("获得钻石："+egg.reward[i][1]);
			rewardsPicpath = gpNamespace.sctc.Sctc_Diamond;
			break;
		case 3:
//			cc.log("获得兑换券："+egg.reward[i][1]);
//			rewardsPicpath = gpNamespace.sctc.Sctc_Voucher;
			if(num < 100){
				rewardsPicpath = gpNamespace.zjd.Zjd_Vouchor2;
			}else{
				rewardsPicpath = gpNamespace.zjd.Zjd_Vouchor4;
			}
			break;
		case 35:
//			cc.log("!!!!!!!!!!!获得周卡："+egg.reward[i][1]);
//			rewardsPicpath = gpNamespace.sctc.Sctc_Gold;
			break;
		case 36:
//			cc.log("!!!!!!!!!!!获得月卡："+egg.reward[i][1]);
//			rewardsPicpath = gpNamespace.sctc.Sctc_Gold;
			break;

		default:
			break;
		}

		var rewardsPic = new cc.Sprite(rewardsPicpath);
		rewardsPic.attr({
			x: 640,
			y: heigth+10,
			anchorX:0.5,
			anchorY:0
		});
		this.addChild(rewardsPic,10);

		var rewardsNum = new tyNamespace.NumLayer(num,35,0,"sz_cdd_zjm_");
		rewardsNum.attr({
			x: rewardsPic.width/2,
			y: -60,
			anchorX:0.5,
			anchorY:0
		});
		rewardsPic.addChild(rewardsNum,10);

		var dhqWord = new cc.Sprite(gpNamespace.zjd.Zjd_dhqWord);
		dhqWord.attr({
			x: rewardsPic.width/2,
			y: -150,
			anchorX:0.5,
			anchorY:0
		});
		rewardsPic.addChild(dhqWord,10);
		/*//奖励图片
    	var rewardsPic = new cc.Sprite(rewardsPicpath);
    	rewardsPic.attr({
    		x:cc.winSize.width/2-30,
    		y:heigth +50
    	});
    	this.addChild(rewardsPic,10);
    	//乘号
    	var mul = new cc.Sprite("#sz_cdd_js_012u.png");
    	mul.attr({
    		x:90,
    		y:35
    	});
    	rewardsPic.addChild(mul);
    	//数量
    	var rewards = new tyNamespace.NumLayer(num.toString(),18,0,"sz_cdd_js_");
    	rewards.attr({
    		x:140,
    		y:35
    	});
    	rewardsPic.addChild(rewards);*/

		this.scheduleOnce(function(){
			rewardsNum.numFadeOut(0.5);
//			rewardsNum.runAction(new cc.FadeOut(0.5));
			dhqWord.runAction(new cc.FadeOut(0.5));
//			mul.runAction(new cc.FadeOut(0.5));
			rewardsPic.runAction(new cc.FadeOut(0.5));
		},2);
	}
});
gpNamespace.RewardLayer =  cc.Layer.extend({
	ctor: function (rewards) {
		this._super();
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.zjdRes.Zjd_plist);

		var rewardsBg = new cc.Sprite(gpNamespace.zjd.Zjd_Rewards_Bg);
		rewardsBg.attr({
			x: 640,
			y: 360
		});
		this.addChild(rewardsBg);

		var bcakBt = new cc.MenuItemImage(gpNamespace.zjd.Zjd_Rewards_BackBt,gpNamespace.zjd.Zjd_Rewards_BackBt,this.backCallback,this);
		bcakBt.attr({
			x: 440,
			y: 34,
			anchorX: 0,
			anchorY: 0
		});
		bcakBt.setUserData(this);
		var backMenu = new cc.Menu(bcakBt); 
		backMenu.attr({
			x: 0,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		rewardsBg.addChild(backMenu,10);

		var rewardsList = new tableViewPro.TableView(this, cc.size(945, 450), 50);//设置tableView大小
		rewardsList.setPosition(200, 200);
		rewardsList.setAnchorPoint(0, 0);
		this.addChild(rewardsList,10);
		rewardsList.addData({
			"barInfo": {"width": 2, "height": 80},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 5},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": 10,
			            		 "imageName": "",
			            		 "height": 190
			            	 }
			             }
			             ]
		});
		rewardsList.showData();

		//cells上加表现
		var cellCount = rewardsList.currentItem.cellInfo.cellCount;
		var cellType = rewardsList.currentItem.cellInfo.cellType;
		var currentPage = rewardsList.currentPage;
		for (var a = 0; a < cellCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
			case 1://menu
//				cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
				break;
			}
			this.setrewardsListkCell(a,cell,rewards);
		}
	},
	backCallback:function(sender){
		var self = sender.getUserData();
		self.getParent().getParent().removeFromParent(true);
	},
	setrewardsListkCell:function(index,cell,rewards){
		var voucherPath = gpNamespace.zjd.Zjd_Vouchor2;
		if(rewards[index][1] > 100){
			voucherPath = gpNamespace.zjd.Zjd_Vouchor4;
		}
		var voucherPic = new cc.Sprite(voucherPath);
		voucherPic.attr({
			x: 0,
			y: 30,
			anchorX:0,
			anchorY:0
		});
		cell.addChild(voucherPic);

		var title = new cc.LabelTTF(rewards[index][1]+"兑换券",gpNamespace.DEFAULTFONT,25);
		title.attr({
			x: 0,
			y: 0,
			anchorX:0,
			anchorY:0
		});
		cell.addChild(title);
	}
})